package com.wsl.mybatiscustomframe.builder;

import com.alibaba.druid.pool.DruidDataSource;
import com.wsl.mybatiscustomframe.io.Resources;
import com.wsl.mybatiscustomframe.pojo.Configuration;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;
import java.util.Properties;

/**
 * packageName com.wsl.mybatiscustomframe.builder  XMLConfigBuilder
 *
 * @author victor
 * @version JDK 8
 * @date 2024/7/11
 * @description TODO
 */
public class XMLConfigBuilder {
    private Configuration configuration;
    public XMLConfigBuilder() {
        configuration = new Configuration();
    }

    public Configuration parse(InputStream inputStream) throws DocumentException {
        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();
        // 解析核心配置文件中数据源部分
        List<Element> list = rootElement.selectNodes("//property");

        Properties properties = new Properties();
        for (Element element : list) {
            String name = element.attributeValue("name");
            String value = element.attributeValue("value");
            properties.setProperty(name,value);
        }
        // 创建数据源对象（连接池）
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(properties.getProperty("driverClassName"));
        druidDataSource.setUrl(properties.getProperty("url"));
        druidDataSource.setUsername(properties.getProperty("username"));
        druidDataSource.setPassword(properties.getProperty("password"));
        // 创建好的数据源对象封装进configuration中、
        configuration.setDataSource(druidDataSource);
        // 解析映射配置文件
        // 1.获取映射配置文件的路径 2.解析 3.封装好mappedStatement
        List<Element> mapperList = rootElement.selectNodes("//mapper");
        for (Element element : mapperList) {
            String mapperPath = element.attributeValue("resource");
            InputStream resourceAsSteam =
                    Resources.getResourceAsSteam(mapperPath);
            XMLMapperBuilder xmlMapperBuilder = new
                    XMLMapperBuilder(configuration);
            xmlMapperBuilder.parse(resourceAsSteam);
        }
        return configuration;
    }

}
